package com.fhminyi.dao;

import com.fhminyi.model.OrderItem;
import com.fhminyi.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class OrderItemDao {
    // 获取所有订单商品
    public List<OrderItem> getAllOrderItems() throws SQLException {
        List<OrderItem> orderItems = new ArrayList<>();
        String sql = "SELECT * FROM 订单商品";
        try (Connection conn = DBUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            while (rs.next()) {
                OrderItem orderItem = new OrderItem();
                orderItem.setOrderItemId(rs.getInt("订单商品_id"));
                orderItem.setOrderId(rs.getInt("订单_id"));
                orderItem.setProductId(rs.getInt("商品_id"));
                orderItem.setQuantity(rs.getInt("数量"));
                orderItem.setPrice(rs.getDouble("价格"));
                orderItems.add(orderItem);
            }
        }
        return orderItems;
    }

    // 根据订单商品ID获取订单商品
    public OrderItem getOrderItemById(int orderItemId) throws SQLException {
        String sql = "SELECT * FROM 订单商品 WHERE 订单商品_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, orderItemId);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    OrderItem orderItem = new OrderItem();
                    orderItem.setOrderItemId(rs.getInt("订单商品_id"));
                    orderItem.setOrderId(rs.getInt("订单_id"));
                    orderItem.setProductId(rs.getInt("商品_id"));
                    orderItem.setQuantity(rs.getInt("数量"));
                    orderItem.setPrice(rs.getDouble("价格"));
                    return orderItem;
                }
            }
        }
        return null;
    }

    // 添加根据订单id获取订单商品列表的方法
    public List<OrderItem> getOrderItemsByOrderId(int orderId) throws SQLException {
        List<OrderItem> orderItems = new ArrayList<>();
        String sql = "SELECT * FROM 订单商品 WHERE 订单_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, orderId);
            try (ResultSet rs = pstmt.executeQuery()) {
                while (rs.next()) {
                    OrderItem orderItem = new OrderItem();
                    orderItem.setOrderItemId(rs.getInt("订单商品_id"));
                    orderItem.setOrderId(rs.getInt("订单_id"));
                    orderItem.setProductId(rs.getInt("商品_id"));
                    orderItem.setQuantity(rs.getInt("数量"));
                    orderItem.setPrice(rs.getDouble("价格"));
                    orderItems.add(orderItem);
                }
            }
        }
        return orderItems;
    }

    // 添加根据商品id获取订单商品列表的方法
    public List<OrderItem> getOrderItemsByProductId(int productId) throws SQLException {
        List<OrderItem> orderItems = new ArrayList<>();
        String sql = "SELECT * FROM 订单商品 WHERE 商品_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, productId);
            try (ResultSet rs = pstmt.executeQuery()) {
                while (rs.next()) {
                    OrderItem orderItem = new OrderItem();
                    orderItem.setOrderItemId(rs.getInt("订单商品_id"));
                    orderItem.setOrderId(rs.getInt("订单_id"));
                    orderItem.setProductId(rs.getInt("商品_id"));
                    orderItem.setQuantity(rs.getInt("数量"));
                    orderItem.setPrice(rs.getDouble("价格"));
                    orderItems.add(orderItem);
                }
            }
        }
        return orderItems;
    }

    // 添加订单商品
    public void addOrderItem(OrderItem orderItem) throws SQLException {
        String sql = "INSERT INTO 订单商品 (订单_id, 商品_id, 数量, 价格) VALUES (?, ?, ?, ?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, orderItem.getOrderId());
            pstmt.setInt(2, orderItem.getProductId());
            pstmt.setInt(3, orderItem.getQuantity());
            pstmt.setDouble(4, orderItem.getPrice());
            pstmt.executeUpdate();
        }
    }

    // 更新订单商品
    public void updateOrderItem(OrderItem orderItem) throws SQLException {
        String sql = "UPDATE 订单商品 SET 订单_id = ?, 商品_id = ?, 数量 = ?, 价格 = ? WHERE 订单商品_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, orderItem.getOrderId());
            pstmt.setInt(2, orderItem.getProductId());
            pstmt.setInt(3, orderItem.getQuantity());
            pstmt.setDouble(4, orderItem.getPrice());
            pstmt.setInt(5, orderItem.getOrderItemId());
            pstmt.executeUpdate();
        }
    }

    // 删除订单商品
    public void deleteOrderItem(int orderItemId) throws SQLException {
        String sql = "DELETE FROM 订单商品 WHERE 订单商品_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, orderItemId);
            pstmt.executeUpdate();
        }
    }
}